/*
 * 1.初始化评论星
 * 2.评论星动画
 * 3.评论输入框的字数限制及剩余字数提示
 * 4.投诉理由输入框的字数限制及剩余字数提示
 * 5.提交评论 rate-btn 都是异步
 * 6.星星评分提交 kartik
 * 7.提交投诉理由 comp-btn
 * 
 */

var ws=new WebSocket("ws://10.222.29.182:8088/m-version3/ws/push");
(function(){
	var obj={};
	
	obj.ajax=function(type,url,data,headers,context,callback){
		$.ajax({type:type,url:url,data:JSON.stringify(data),headers:headers,context:context})
		.done(callback);
	};
	window.j=obj;
})();

function CommentComponent(template){
	this.model=null;
	
	this.template=template;

	//提交评论及评分
	this.submitCmt=function(cmtinfo){
		j.ajax("POST",
				"cmt",
				cmtinfo,
				{"Content-Type":"application/json"},
				this,
				function(ordercmt){
					this.model=ordercmt;
					cmt_render(this.model);
				});
	}
	
	//点击提交投诉按钮，发送投诉理由过去，返回时关闭模态框，按钮成为禁用状态
	this.submitComp=function(compreason){
		$.ajax({
			type:"POST",
			url:"comp",
			data:compreason
		}).done(function(){
			$("#myModal").modal("hide");
			$("#compbtn").attr("disabled","disabled");
		})
	}
	
	this.confirmOrder=function(){
		$.ajax({
			type:"GET",
			url:"comfirmorder"
		}).done(function(){
			$("#confirmModal").modal("hide");
			$("#confirmdiv").empty();
			
		})
	}
	
	function formatDate(time){
		var now = new Date(time);     
		var year=now.getFullYear();
        var month=now.getMonth()+1;
        if(month<10)
        	month="0"+month;
        var date=now.getDate();
        if(date<10)
        	date="0"+date;
        var hour=now.getHours();
        var minute=now.getMinutes(); 
        var second=now.getSeconds();
        return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;   
	}
	function cmt_render(data){
		template.empty();
		var outter1=$("<div></div>").addClass("ordercomment");
		var outter2=$("<div></div>").addClass("panel panel-success");
		var outter31=$("<div></div>").addClass("panel-heading");
		var outter32=$("<div></div>").addClass("panel-body");
		var ul=$("<ul></ul>").addClass("list-group");
		var cmtli=$("<li></li>").addClass("list-group-item").attr("id","cmt");
		alert("time..."+data.ctime);
		
//		var timespan=$("<span></span>").addClass("cmt-time").html(formatDate(data.ctime));
		alert("shijian......"+formatDate(data.ctime));
		var time = formatDate(data.ctime);
		
		var p1=$("<p></p>").html("您在"+time+"发表了评论：");
		alert(data.comments);
		var pcmt=$("<p></p>").addClass("publish").text(data.comments);
		
		cmtli.append(p1);
		cmtli.append(pcmt);
		ul.append(cmtli);
		outter32.append(ul);
		outter2.append(outter31);
		outter2.append(outter32);
		outter1.append(outter2);
		outter1.appendTo(template);
		
	}
}

$(function(){
	alert("comein");
	var o=new CommentComponent($(".ordercomment"));
	
	//评价星初始化
    $("#rate-submit").on("click",function(){
        var rate=$("#kartik").val();
        alert(rate);
    });

    
    //评论输入框字数限制及剩余字数提示
    $("#cmttext").keyup(function(){
        //alert("hello");
        var len=$(this).val().length;
        if(len>49){
            $(this).val($(this).val().substring(0,50));
        }
        var num=50-len;
        $("#word").text(num);
    });
    
    // 投诉输入框字数限制及剩余字数提示 
    $("#comptext").keyup(function(){
        //alert("hello");
        var len=$(this).val().length;
        if(len>99){
            $(this).val($(this).val().substring(0,100));
        }
        var num=100-len;
        $("#compword").text(num);
    });
	
    $("#rate-btn").on("click",function(e){
    	alert("come");
    	var len=$("#cmttext").val().length;
    	if(len<1){
    		alert("评论不能为空");
            $("#cmtwarning").text("评论不能为空");
            return;
        }
    	var grade=$("input[name=grade]","#star-rating").val();
    	alert(grade);
    	var comment=$("#cmttext").val();
    	alert(comment);
    	var oid=$("input[name=oid]",".fillcmt").val();
    	alert(oid);
    	var rate={"oid":oid,"comments":comment,"oclevel":grade};
    	o.submitCmt(rate);
    });
	
    //模态框里的投诉提交按钮
    $("#submitcomp").on("click",function(e){
    	var len=$("#comptext").val().length;
    	if(len<1){
            $("#compwarning").text("投诉理由不能为空");
            return;
        }
        var num=100-len;
    	var oid=$("input[name=oid]",".complaint").val();
    	alert(oid);
    	var complaint=$("#comptext").val();
    	alert(complaint);
    	var compobj={"oid":oid,"compreason":complaint};
    	o.submitComp(compobj);
    	
    });
    
    $("#compbtn").on("click",function(e){
    	$("#myModal").modal("show");
    });
	
    //确认收货模态框弹出
    $("#confirmalert").on("click",function(e){
    	$("#confirmModal").modal("show");
    });
    
    //确认收货按钮推送消息
    $("#confirmbtn").on("click",function(e){
    	window.location.href="confirmorder";
    });
    
    //取消订单时模态框弹出
    $("#cancelalert").on("click",function(e){
    	$("#cancelModal").modal("show");
    });
    
    //取消订单按钮推送消息
    $("#cancelbtn").on("click",function(e){
    	var icnum=$("#icnum").val();

    	alert(icnum);
    	ws.send(icnum+"_300");//取消订单发送300消息给M
    	
    	window.location.href="cancelorder";
    	
    });
})



